package com.splant.android.smart.UILevel;

import android.app.Activity;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.RadioButton;
import android.widget.RadioGroup;

import com.splant.android.smart.Global;
import com.splant.android.R;
import com.splant.android.smart.SystemDefinedLevel.Utils.System_Util;
import com.splant.android.smart.UILevel.Community.Garden_Base_Activity;

import java.util.Locale;

import de.greenrobot.event.EventBus;

public class System_LanguageSelect_Activity extends Garden_Base_Activity implements View.OnClickListener {

    private Button btn_goBack;
    private RadioGroup rdoGroup_selectLanguage;
    private Button btn_enter;

    private RadioButton rdoBtn_auto;
    private RadioButton rdoBtn_simplified_chinese;
    private RadioButton rdoBtn_english;

    public final static String LANGUAGE_CHANGED = "languageChanged";

    @Override
    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);

        setContentView(R.layout.system_languageselect_activity);

        System_Util.configLanguage(this);


        btn_goBack = (Button) findViewById(R.id.btn_goBack);
        rdoGroup_selectLanguage = (RadioGroup) findViewById(R.id.rdoGroup_selectLanguage);
        rdoBtn_auto = (RadioButton) findViewById(R.id.rdoBtn_auto);
        rdoBtn_simplified_chinese = (RadioButton) findViewById(R.id.rdoBtn_simplified_chinese);
        rdoBtn_english = (RadioButton) findViewById(R.id.rdoBtn_english);
        btn_enter = (Button) findViewById(R.id.btn_enter);

        SharedPreferences sharedPreferences = getSharedPreferences(System_Util.LANGUAGECONFIG_NAME, Activity.MODE_PRIVATE);
        String language = sharedPreferences.getString(System_Util.LANGUAGECONFIG_LANGUAGE, Locale.ENGLISH.getLanguage());
        boolean isAuto = sharedPreferences.getBoolean(System_Util.LANGUAGECONFIG_ISAUTO, true);

        String simplifiedChinese = Locale.SIMPLIFIED_CHINESE.getLanguage();
        String english = Locale.ENGLISH.getLanguage();

        if (isAuto)
        {
            mLocale = Locale.getDefault();

            language = mLocale.getLanguage();

            if (!language.equals(simplifiedChinese)) {
                mLocale = new Locale(english);
            }

            rdoBtn_auto.setChecked(true);
        }
        else {

            mLocale = new Locale(language);

            if (language.equals(simplifiedChinese))
                rdoBtn_simplified_chinese.setChecked(true);
            else {
                mLocale = new Locale(english);
                rdoBtn_english.setChecked(true);
            }

        }

        System_Util.configLanguage(this, mLocale);

        rdoGroup_selectLanguage.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup radioGroup, int i) {
                selectLanguage(i);
            }
        });

        btn_enter.setOnClickListener(this);
        btn_goBack.setOnClickListener(this);
    }

    private Locale mLocale;

    private void enterSelectLanguage() {

        System_Util.configLanguage(this, mLocale);

        SharedPreferences.Editor editor = getSharedPreferences(System_Util.LANGUAGECONFIG_NAME, Activity.MODE_PRIVATE).edit();

        String language = mLocale.getLanguage();

        editor.putString(System_Util.LANGUAGECONFIG_LANGUAGE, language);

        editor.putBoolean(System_Util.LANGUAGECONFIG_ISAUTO, rdoBtn_auto.isChecked());

        boolean b = editor.commit();

        if (b)
        {

            if (mLocale==Locale.SIMPLIFIED_CHINESE)
                Global.language=1;
            else
                Global.language=2;

          //  EventBus.getDefault().post(new LanguageChange());

            finish();
        }
    }

    private void selectLanguage(int rdoId) {
        switch (rdoId) {

            case R.id.rdoBtn_simplified_chinese:
                mLocale = Locale.SIMPLIFIED_CHINESE;
                break;
            case R.id.rdoBtn_english:
                mLocale = Locale.ENGLISH;
                break;
            default:
                mLocale = Locale.SIMPLIFIED_CHINESE;
                break;

        }
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.btn_goBack:
                finish();
                break;
            case R.id.btn_enter:
                enterSelectLanguage();
                break;
        }
    }


}
